package com.artPreter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.artPreter.mapper.DepartMapper;
import com.artPreter.pojo.Depart;
import com.artPreter.pojo.DepartTemp;
import com.artPreter.mapper.DepartTempMapper;
import com.artPreter.service.IDepartTempService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 艺术机构模板表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-18
 */
@Service
public class DepartTempServiceImpl extends ServiceImpl<DepartTempMapper, DepartTemp> implements IDepartTempService {

    @Autowired
    private DepartMapper departMapper;

    /**
     * 获取机构模板，如果没有模板则添加一个新模板
     * @param departTemp
     * @return
     */
    @Override
    public DepartTemp getDepartTemp(DepartTemp departTemp) {
        LambdaQueryWrapper<DepartTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DepartTemp::getDepartId,departTemp.getDepartId());
        List<DepartTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
        System.out.println(departTemp.getDepartId());
        if (list.size() == 0){
            DepartTemp result = new DepartTemp();
            String uuid = java.util.UUID.randomUUID().toString();
            result.setContentId(uuid);
            LambdaQueryWrapper<Depart> departLambdaQueryWrapper = new LambdaQueryWrapper<>();
            departLambdaQueryWrapper.eq(Depart::getDepartId,departTemp.getDepartId());
            List<Depart> departList = departMapper.selectList(departLambdaQueryWrapper);
            if (departList.size() > 0){
                result.setName(departList.get(0).getName()+"模板");
                result.setDepartId(departList.get(0).getDepartId());
            }
            this.baseMapper.insert(result);
            return result;
        }else{
            return list.get(0);
        }

    }

    @Override
    public void saveDepartTemp(DepartTemp departTemp) {
        this.baseMapper.updateById(departTemp);
    }
}
